/*
 * This file is part of the Sx Framework Library.
 * 
 * Copyright (C) 2013 University of Colorado Denver
 * <min.choi@ucdenver.edu>
 * <shane.transue@ucdenver.edu>
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy 
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 
 * copies of the Software, and to permit persons to whom the Software is 
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in 
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
 * DEALINGS IN THE SOFTWARE.
 */
#include <sxQDebugStream.h>
#include <sxQConsole.h>

const static char ASCII_NEWLINE = '\n';

Sx::QtInterface::System::QDebugStream::QDebugStream(std::ostream* stream, QConsole* console, const String& logFilename, bool append) {
	this->console = console;
	this->stream = stream;

	if ( this->stream != nullptr ) {
		this->originalBuf = this->stream->rdbuf();
		this->stream->rdbuf(this);
	}

	this->logFilename = logFilename;

	connect(this, SIGNAL(onAppend(const QString&)), this->console, SLOT(onAppend(const QString&)));
	connect(this, SIGNAL(onLogAppend(const QString&)), this->console, SLOT(onLogAppend(const QString&)));
}

Sx::QtInterface::System::QDebugStream::~QDebugStream() {}

const Sx::String& Sx::QtInterface::System::QDebugStream::getLogFilename() const {
	return this->logFilename;
}

void Sx::QtInterface::System::QDebugStream::setStream(std::ostream* stream) {
	if ( stream == nullptr ) return;

	//--------------------------------------------------------------------------
	// Restore the previous streams buffer
	//--------------------------------------------------------------------------
	if ( this->stream != nullptr ) this->stream->rdbuf(this->originalBuf);
	this->originalBuf = stream->rdbuf();
	//--------------------------------------------------------------------------
	// Redirect the streams buffer to this debug stream
	//--------------------------------------------------------------------------
	this->stream = stream;
	this->stream->rdbuf(this);
}

void Sx::QtInterface::System::QDebugStream::setLogFilename(const String& filename) {
	this->logFilename = filename;
}

Sx::QtInterface::System::int_type Sx::QtInterface::System::QDebugStream::overflow(int_type character) {
	if ( this->stream == nullptr ) return 0;

	if ( character == ASCII_NEWLINE ) {
		emit onAppend(QString(this->data.toAscii().constData()));
		emit onLogAppend(QString(this->data.toAscii().constData()));
		this->data.clear();
	}
	else this->data.append(static_cast<char>(character));
	return character;
}

std::streamsize Sx::QtInterface::System::QDebugStream::xsputn(const char* p, std::streamsize n) {
	if ( p == 0 || n == 0 || this->stream == nullptr ) return 0;
	this->data.append(p);

	int pos = 0;
	while ( pos != String::INVALID_STRING_POSITION ) {
		pos = this->data.findFirstInstanceOf(ASCII_NEWLINE);

		if ( pos != String::INVALID_STRING_POSITION ) {
			String s = this->data.substring(0, pos);
			emit onAppend(QString(s.toAscii().constData()));
			emit onLogAppend(QString(s.toAscii().constData()));
			this->data.eraseSubstring(0, pos + 1);
		}
	}

	return n;
}
